//==========================================================================
// Copyright (c) 2000-2008,  Elastos, Inc.  All Rights Reserved.
//==========================================================================

#ifndef __ELASTOS_DDK_H__
#define __ELASTOS_DDK_H__

#define _NO_INCLIST
#include <eltypes.h>
#include <elasys_server.h>
#include <interlocked.h>

#include <driver.h>
#include <stdio.h>
#include <_pubcrt.h>
#include <eventman.h>

//#define kprintf printf
#define Thread IThread
#define ScheduleClass void

#ifdef __cplusplus
extern "C" int    __cdecl kprintf (const char *, ...);
#else
int    __cdecl kprintf (const char *, ...);
#endif

#define  PHYSADDR(addr)  addr
#define g_pDefaultScheduleClass NULL
#define g_uDefaultSchedulePolicy NULL
#define PAGE_SIZE                       4096
#define _MAX_FNAME                      256



class DzMutex {
public:
    DzMutex();
    ~DzMutex();
    int Lock(int *result);
    int Unlock();
    void* GetMutex();
private:
    void* m_mutex;
};

class DzCondition {
public:
    DzCondition();
    ~DzCondition();
    int Pulse();
    int TryWait(DzMutex *, int, int *);
    int Wait(DzMutex*, int *);
private:
    void* m_cond;
};


enum
{
    CreateThreadFlag_System     = 0x80,
};
typedef Int32 CreateSThreadFlag;

void DzSleep(uint_t uTicks, WaitResult *pResult);

#define KCONFIG_NUMBEROF_ORDERS         10
#define c_uNumberOfOrders  KCONFIG_NUMBEROF_ORDERS

EXTERN_C int DzAcquireThreadFieldSlot(unsigned int *);
EXTERN_C void** DzGetThreadFieldAddress(unsigned int uIndex);
EXTERN_C int DzSetSlotValue(unsigned int uIndex, void* pValue);

void *DzAllocKernelPages(uint_t uOrder);

void DzFreeKernelPages(virtaddr_t va, uint_t uOrder);

EXTERN void DzGetSystemTime(SystemTime *pSystemTime);

interface IStub;

EXTERN ECode DzRegisterService(
        const wchar_t *pwszServiceName, PInterface pObj, IStub **ppIStub);

EXTERN ECode DzUnregisterService(const wchar_t *pwszName, IStub *pIStub);

EXTERN ECode DzFindService(const wchar_t *pwszName, PInterface *ppObj);

typedef Int32 (CDECL *threadroutine_t)(void *);

EXTERN ECode DzCreateKernelThread(
        threadroutine_t threadroutine, void *pvArg, UInt32 dwFlags,
        ThreadPriorityRank priorityRank, IThread **ppThread);

EXTERN_C void * Drv_AllocBuffer(unsigned int uPages, unsigned int uFlag);
EXTERN_C void Drv_FreeBuffer(void * VirtAddr, unsigned int uPages);

EXTERN ECode DzIoRemap(
         Address virtualAddr,
         MemorySize size,
         Address physicalAddr,
         MemoryMapFlags flags,
         MemoryProtection protection,
         Address *pVirtualAddr);

EXTERN ECode DzIoUnmap(
         Address baseAddr,
         MemorySize size);

EXTERN ECode DzCreateEventObj(
         Boolean manualReset,
         EventState initialState,
         IEvent **ppEvent);
EXTERN IThread *CreateSystemThread(
        threadroutine_t threadroutine, void *pvParameter,
        void *pScheduleClass, uint_t uSchedulePolicy);

EXTERN UInt32 DzMillisecondsToTicks(UInt32 milliseconds);
EXTERN UInt32 DzTicksToMilliseconds(UInt32 time);
EXTERN UInt32 DzGetElapsedTicks();

EXTERN_C ECode __cdecl OpenShareMem(unsigned long id, Address *pShareAddr);
EXTERN_C ECode __cdecl FreeOpenShareMem(Address ShareAddr);
EXTERN_C ECode __cdecl AllocShareMem(MemorySize size,MemoryProtection protection,Address *pShareAddr,unsigned long *pId);
EXTERN_C ECode __cdecl FreeAllocShareMem(Address ShareAddr);
EXTERN_C ECode __cdecl ConvertAllocAddrToId(Address ShareAddr,unsigned long offset, unsigned long *pId);

#endif
